<?php

namespace app\api\controller\web\frontend;

use app\common\controller\Api;
use think\facade\Db;

// 政策法规公告
class Policy extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    public function list()
    {
        // 分类
        $type = input('type') ?? 1;
        $keywords = input('keywords') ?? '';
        $where = [];
        if ($type) {
            $where[] = ['state', '=', $type];
        }
        if ($keywords) {
            $where[] = ['title', 'like', '%'.$keywords.'%'];
        }

        // 添加必要判断
        $where[] = ['is_showswitch','=',1];
        $where[] = ['deletetime','=',null];

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;
        
        $list = Db::name('web_policy')
                        ->field('id, title, state, online_time, content,tpic_image')
                        ->where('online_time <= '.time())
                        ->where($where)
                        ->order('weigh desc, online_time desc')
                        ->limit($start, $page_size)
                        ->select()
                        ->toArray();
        
        foreach ($list as $key => $value) {
            $list[$key]['online_time'] = date('Y-m-d', $value['online_time']);
            $list[$key]['content'] = strip_tags(htmlspecialchars_decode($value['content']));
            $list[$key]['content'] = preg_replace("/&nbsq;/", " ", $list[$key]['content']);
            $list[$key]['content'] = preg_replace("/&nbsp;/", " ", $list[$key]['content']);
        }
        
        $count = Db::name('web_policy')->where($where)->where('online_time < '.time())->count();

        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);
    }


    /**
     * 政策法规详情
     */
    public function info()
    {
        $id = input('policy_id');
        if (!$id) {
            $this->error('请求参数出错');
        }

        $data = Db::name('web_policy')->field('id, title, state, online_time, content,tpic_image')->find($id);
        if (empty($data)) {
            $this->error('不存在的数据');
        }

        // 数据处理
        $data['online_time'] = date('Y-m-d', $data['online_time']);
        // 用于处理微信图片不能查看的问题
        $data['content'] = str_replace("mmbiz.qpic.cn","wximg.cccyun.cc", $data['content']);
        $data['content'] = str_replace("/uploads","https://admin.fs-hitech.com/uploads", $data['content']);
        $data['content'] = htmlspecialchars_decode($data['content']);

        // 上一篇 下一篇
        // 查所有 取索引（后期数据量大 卡慢）
        $data['prev'] = Db::name('web_policy')->where('state', $data['state'])->where('id', '<', $id)->max('id');
        $data['next'] = Db::name('web_policy')->where('state', $data['state'])->where('id', '>', $id)->min('id');

        $this->success('success',$data);

    }



}